//initialization for Thor PIC
#include <p18f4580.h>
#include <stdio.h>
#include "PIC_constants.h"

void init_PIC(void){

	TRISB = 0;								//set Port B to output
	PORTB = 0b11100000;						//turn LEDs off

    TRISE = 0;                           	//set Port E to output           
    PORTE = 255;                            //turn LED on
    
	//initialize Port C and disable multiplexed devices on Port C
    TRISC = 0b10000000;                     //set Port C to output
    SSPCON1bits.SSPEN=0;                    //turn off MSSP device
	CCP1CON=0;								//disable PWM/CCP module
	T1CON=0;								//disable Timer 1   

	//initialize USART
	RCSTAbits.SPEN = 1;
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF &
				USART_ASYNCH_MODE & USART_EIGHT_BIT &
				USART_CONT_RX & USART_BRGH_HIGH,
				10); //baud rate 115200
				//64); //baud rate 19200

    //configure Timer 0, set prescaler to 1:1
    OpenTimer0(TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT & T0_PS_1_1);

    INTCON2bits.TMR0IP = 0;
    RCONbits.IPEN=1;
    INTCONbits.GIE=1;
    INTCONbits.PEIE=1;
    INTCONbits.INT0IE = 0;					//disable external interrupts 
    INTCON2bits.INTEDG0 = 0;  

	//turn all motors off
	PORTCbits.RC0=0;
    PORTCbits.RC2=0;
	PORTCbits.RC6=0;
	PORTCbits.RC7=0; 
	
	//write timer offset value
    WriteTimer0(timer_offset);

}
